package com.edu.exam.entity.assessment.standard;

import com.edu.exam.enums.assessment.StandardType;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 成绩评定标准实体类
 * 所属模块：F-07 基础数据管理模块
 * 对应数据库表：grading_standards
 *
 * 管理成绩评定的标准和规则，包含等级划分、及格线等。
 * 支持成绩转换和评定标准验证。
 *
 * @author System
 * @version 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "grading_standards")
@Comment("成绩评定标准表")
public class GradingStandard {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 标准编码
     */
    @NotBlank(message = "标准编码不能为空")
    @Size(max = 30, message = "标准编码长度不能超过30个字符")
    @Column(name = "standard_code", nullable = false, unique = true, length = 30)
    private String standardCode;

    /**
     * 标准名称
     */
    @NotBlank(message = "标准名称不能为空")
    @Size(max = 100, message = "标准名称长度不能超过100个字符")
    @Column(name = "standard_name", nullable = false, length = 100)
    private String standardName;

    /**
     * 标准类型
     */
    @NotNull(message = "标准类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "standard_type", nullable = false, length = 20)
    private StandardType standardType;

    /**
     * 关联考核方式代码
     */
    @Size(max = 50, message = "关联考核方式代码长度不能超过50个字符")
    @Column(name = "assessment_method_code", length = 50)
    private String assessmentMethodCode;

    /**
     * 等级制类型
     */
    @Size(max = 20, message = "等级制类型长度不能超过20个字符")
    @Column(name = "grading_scale", length = 20)
    private String gradingScale;

    /**
     * 及格等级
     */
    @Size(max = 10, message = "及格等级长度不能超过10个字符")
    @Column(name = "passing_grade", length = 10)
    private String passingGrade;

    /**
     * 优秀等级
     */
    @Size(max = 10, message = "优秀等级长度不能超过10个字符")
    @Column(name = "excellent_grade", length = 10)
    private String excellentGrade;

    /**
     * 生效日期
     */
    @Column(name = "effective_date")
    private LocalDate effectiveDate;

    /**
     * 失效日期
     */
    @Column(name = "expiry_date")
    private LocalDate expiryDate;

    /**
     * 是否当前标准
     */
    @Column(name = "is_current_standard")
    @Builder.Default
    private Boolean isCurrentStandard = false;

    /**
     * 是否激活
     */
    @Column(name = "is_active")
    @Builder.Default
    private Boolean isActive = true;

    /**
     * 描述信息
     */
    @Size(max = 1000, message = "描述信息长度不能超过1000个字符")
    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }

    /**
     * 验证等级制
     */
    public boolean validateGradingScale() {
        return gradingScale != null && !gradingScale.trim().isEmpty();
    }

    /**
     * 判断是否为及格成绩
     */
    public boolean isPassingGrade(String grade) {
        if (passingGrade == null || grade == null) {
            return false;
        }
        // 简单的字符串比较，实际应用中可能需要更复杂的逻辑
        return grade.equalsIgnoreCase(passingGrade);
    }

    /**
     * 判断是否为优秀成绩
     */
    public boolean isExcellentGrade(String grade) {
        if (excellentGrade == null || grade == null) {
            return false;
        }
        return grade.equalsIgnoreCase(excellentGrade);
    }

    /**
     * 转换为字母等级（简化版）
     */
    public String convertToLetterGrade(Double score) {
        if (score == null) {
            return null;
        }

        if (score >= 90) return "A";
        if (score >= 80) return "B";
        if (score >= 70) return "C";
        if (score >= 60) return "D";
        return "F";
    }

    /**
     * 判断标准是否当前有效
     */
    public boolean isCurrentlyEffective() {
        LocalDate now = LocalDate.now();
        boolean afterEffective = effectiveDate == null || !now.isBefore(effectiveDate);
        boolean beforeExpiry = expiryDate == null || !now.isAfter(expiryDate);
        return afterEffective && beforeExpiry;
    }
}